﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseObj;

public partial class Controls_Login : System.Web.UI.UserControl, IInterFaceControl
{
    private string controlId = "40E516AB-94DD-436D-81CA-A9FFE0FACDC1";
    private string controlName = "用户登录控件";
    //private string LoginUrl = "../manage/Manage.aspx";

    /// <summary>
    /// 控件名称（存储到数据库）
    /// </summary>
    public string ControlName
    {
        get { return controlName; }
    }

    /// <summary>
    /// 控件的Id（存储到数据库）
    /// </summary>
    public string ControlId
    {
        get { return controlId; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 登录事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        User ur = new User(this.txtUserId.Text);
        if (ur.IsEffective)
        {
            if (ur.Password == this.txtPassword.Text)
            {
                //HttpCookie cookie = new HttpCookie("UserId",ur.UserId);
                //DateTime dt = DateTime.Now.Date;
                //TimeSpan ts = new TimeSpan(1, 0, 0);//一小时
                //cookie.Expires = dt.Add(ts);
                //Response.AppendCookie(cookie);
                Page.Session["userLoginId"] = ur.UserLoginId;
                ur.UpdateLogin(ur.UserLoginId);//更新用户登录
                this.Response.Write("<script>javascript:window.location.href=\"" + ur.UserGroup.LoginUrl + "\"</script>");
            }
            else
            {
                this.Response.Write("<script>javascript:alert(\"密码错误！请确认是否开启了Caps Lock。\");</script>");
            }
        }
        else
        {
            this.Response.Write("<script>javascript:alert(\"没有这个用户！或您尚未获得系统角色\")</script>");
        }
    }

    /// <summary>
    /// 用户是否可以使用此控件
    /// </summary>
    /// <returns></returns>
    public bool VerifyUser()
    {
        return true;
    }
}